package com.sgcc.fj.pmos.common.utils;


import android.app.Dialog;
import android.graphics.Color;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.blankj.utilcode.util.ActivityUtils;
import com.sgcc.pmos.nativesdk.R;


import java.util.List;

public class OptionPickerUtil<T> {
    private int itemsVisibleCount = 5;
    private int textColorConfirm = -16417281;
    private int textColorCancel = -16417281;
    private OnOptionsSelectListener onOptionsSelectListener;
    private String titleText;
    private OptionsPickerView pvOptions;
    private int mSelectIndext = 0;
    private int bgOutSideColor = -1;

    public synchronized static OptionPickerUtil getInstance() {
        return new OptionPickerUtil();
    }

    public void setItemsVisibleCount(int itemsVisibleCount) {
        this.itemsVisibleCount = itemsVisibleCount;
    }

    public OptionPickerUtil setOnOptionsSelectListener(OnOptionsSelectListener onOptionsSelectListener) {
        this.onOptionsSelectListener = onOptionsSelectListener;
        return this;
    }

    public OptionPickerUtil setTitleText(String titleText) {
        this.titleText = titleText;
        return this;
    }

    public OptionPickerUtil setSelectOptions(int selectOptions) {
        this.mSelectIndext = selectOptions;
        return this;
    }

    public OptionPickerUtil setTextColorConfirm(int textColorConfirm) {
        this.textColorConfirm = textColorConfirm;
        return this;
    }

    public OptionPickerUtil setOutSideColor(int outSideColor) {
        this.bgOutSideColor = outSideColor;
        return this;
    }

    public OptionPickerUtil setTextColorCancel(int textColorCancel) {
        this.textColorCancel = textColorCancel;
        return this;
    }

    public OptionPickerUtil setLayoutRes(int res, CustomListener listener) {
        this.setLayoutRes(res, listener);
        return this;
    }

    public OptionsPickerView getOptionsPicker() {
        return pvOptions;
    }

    public OptionPickerUtil build() {
        pvOptions = new OptionsPickerBuilder(ActivityUtils.getTopActivity(), onOptionsSelectListener)
                .setItemVisibleCount(itemsVisibleCount)
                .setSubCalSize(16)//确定和取消文字大小
                .setTitleText(titleText)
                .setSubmitText("确认")
                .setCancelText("取消")
                .setOutSideColor(bgOutSideColor)
                .setTitleColor(ActivityUtils.getTopActivity().getResources().getColor(R.color._666666))
                .setSubmitColor(textColorConfirm < 0 ? ActivityUtils.getTopActivity().getResources().getColor(R.color._09B1FF) : textColorConfirm)//确定按钮文字颜色
                .setCancelColor(textColorCancel < 0 ? ActivityUtils.getTopActivity().getResources().getColor(R.color._555555) : textColorCancel)//取消按钮文字颜色
                .setTextColorCenter(Color.BLACK)//设置选中项的颜色
                .setBgColor(Color.WHITE)
                .setTitleSize(14)
                .setSelectOptions(mSelectIndext)
                .isRestoreItem(true)//切换时是否还原，设置默认选中第一项。
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .build();

        return this;
    }

    public void setPicker(List<T> optionsItems) {
        this.setPicker(optionsItems, (List) null, (List) null);
    }

    public void setPicker(List<T> options1Items, List<List<T>> options2Items) {
        this.setPicker(options1Items, options2Items, (List) null);
    }

    public void setPicker(List<T> options1Items, List<List<T>> options2Items, List<List<List<T>>> options3Items) {
        if (pvOptions == null) {
            Log.i("OptionPickerUtil", "请先执行build 在执行setpicker");
            return;
        }
        pvOptions.setPicker(options1Items, options2Items, options3Items);
        show(pvOptions);
    }

    /**
     * @author yinsh
     * @description 显示时间选择器
     * @date 2021/7/9 上午9:55
     */
    private void show(OptionsPickerView pvOptions) {
        Dialog mDialog = pvOptions.getDialog();
        if (mDialog != null) {
            Window dialogWindow = mDialog.getWindow();
            if(dialogWindow!=null) {
                dialogWindow.setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL);
                dialogWindow.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
                dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
                WindowManager.LayoutParams layoutParams = dialogWindow.getAttributes();
                layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
                layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    // 延伸显示区域到刘海
                    WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                    lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
                    dialogWindow.setAttributes(lp);
                    // 设置页面全屏显示
                    final View decorView = dialogWindow.getDecorView();
                    decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                }
                dialogWindow.setAttributes(layoutParams);
            }
        }
        pvOptions.show();
    }
}
